#!/usr/bin/env python
# encoding: utf-8
'''
Utils.TopicFormater -- shortdesc

Utils.TopicFormater is a description

It defines classes_and_methods

@author:     Anson Chan

@copyright:  2016 Xerland Co. Ltd. All rights reserved.

@contact:    anson_chan@xerland.com
@deffield    updated: Updated
'''
from Utils.SysConstants import SERVERAPI_CONFIGSERVICE
USER_PREFIX = "/user/"
USER_TOPIC_DIVIDER = "@"
REQUEST_SUFFIX = "/request"
RESPONSE_SUFFIX = "/response"
WILDCARD_SUFFIX = "/#"

class TopicFormater():
    '''
    classdocs
    '''
    baseTopic = SERVERAPI_CONFIGSERVICE.getValue("mq_subscription_base") 
    selfId =  USER_PREFIX+SERVERAPI_CONFIGSERVICE.getValue("mq_client_id")
    hostname = SERVERAPI_CONFIGSERVICE.getValue("mq_hostname")
    hostport = SERVERAPI_CONFIGSERVICE.getValue("mq_port")
    
    def __init__(self):
        '''
        Constructor
        '''

        
        
#    def getSubFrom(self, content):
#        return self.baseTopic+content+USER_TOPIC_DIVIDER+self.peerId
#    
#    def getSubTo(self, content):
#        return self.selfId+USER_TOPIC_DIVIDER+self.baseTopic+content
#    
#    def getSubToFrom(self, content):
#        return self.selfId+USER_TOPIC_DIVIDER+self.baseTopic+content+USER_TOPIC_DIVIDER+self.peerId
#    
#    def getSubToFromX(self, content):
#        return self.selfId+USER_TOPIC_DIVIDER+self.baseTopic+content+REQUEST_SUFFIX+USER_TOPIC_DIVIDER+self.peerId
#    
#    def getPubFrom(self, content):
#        return self.baseTopic+content+USER_TOPIC_DIVIDER+self.selfId
#    
#    def getPubToFrom(self, content):
#        return self.peerId+USER_TOPIC_DIVIDER+self.baseTopic+content+USER_TOPIC_DIVIDER+self.selfId
#    
#    def getPubToFromX(self, content):
#        return self.peerId+USER_TOPIC_DIVIDER+self.baseTopic+content+self.RequestSuffix+USER_TOPIC_DIVIDER+self.selfId
    
    
    @staticmethod
    def getSubBaseWildcard():
        return TopicFormater.selfId+USER_TOPIC_DIVIDER+TopicFormater.baseTopic+WILDCARD_SUFFIX
    
    @staticmethod
    def getSubSelf( content):
        return TopicFormater.selfId+USER_TOPIC_DIVIDER+TopicFormater.baseTopic+content
    
#    @staticmethod
#    def getSubSelfPeer( content):
#        return TopicFormater.selfId+USER_TOPIC_DIVIDER+TopicFormater.baseTopic+content+USER_TOPIC_DIVIDER+TopicFormater.peerId
#    
    @staticmethod
    def getSubSelfPeerX( content):
        return TopicFormater.selfId+USER_TOPIC_DIVIDER+TopicFormater.baseTopic+content+REQUEST_SUFFIX
    
    @staticmethod
    def getPubSelf( content):
        return TopicFormater.baseTopic+content+USER_TOPIC_DIVIDER+TopicFormater.selfId
        
    @staticmethod
    def reverseSubNPub(topic):
        topic = topic.replace(REQUEST_SUFFIX,RESPONSE_SUFFIX)
        if topic.count('@')<2:
            return topic
        if topic.count('@') == 2:
            return topic.split('@')[-1]+'@'+topic.split('@')[1]+'@'+topic.split('@')[0]
        topic = topic.split('@')[-1]+'@'+'@'.join(topic.split('@')[1:-1])+'@'+topic.split('@')[0]
        return topic
    
    
    @staticmethod
    def isEqual(topic1, topic2):
        if len(topic1) > len(topic2):
            if topic1[0:len(topic2)] == topic2:
                return True
        else:
            if topic1 == topic2[0:len(topic1)]:
                return True
        return False
    
    @staticmethod
    def getHostName():
        return TopicFormater.hostname
    
    @staticmethod
    def getHostPort():
        return int(TopicFormater.hostport)
    
    @staticmethod
    def getClientId():
        return TopicFormater.selfId
    
    
        
        
if __name__ == '__main__':
#    tpFormater = TopicFormater("/saibao/led", "cafe")        
#    print tpFormater.getPubFrom("/powerstate/update")
#    print tpFormater.getPubFrom("/rtd/update")
#    print tpFormater.getSubToFromX("/rtd")
#    print tpFormater.getSubToFromX("/powerstate")
#    print tpFormater.getSubToFrom("/rtd")
#    print tpFormater.getSubToFrom("/powerstate")
#    print tpFormater.getSubFrom("/rtd/update")
#    print tpFormater.getSubFrom("/powerstate/update")
#    print '..........'
#    print tpFormater.getSubTo("")
    print TopicFormater.getSubBaseWildcard()
    print TopicFormater.getSubSelf('/rtd')
#    print TopicFormater.getSubSelfPeer('/powerstate')
#    print TopicFormater.getSubSelfPeerX('/rtd')
    print TopicFormater.getPubSelf('/powerstate/update')
    
    print TopicFormater.reverseSubNPub("111@222@333")
    print TopicFormater.reverseSubNPub("111@222")
    print TopicFormater.reverseSubNPub("111")
    print TopicFormater.reverseSubNPub("111@222@333@444")
    print TopicFormater.isEqual("111","1112")
    print TopicFormater.isEqual("111233","1112")
    
    print TopicFormater.getSubSelfPeerX("/powerstate")
    print TopicFormater.getPubSelf("/powerstate/update")
    